package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.CheckingManage;
import cn.wolfcode.crm.query.CheckingManageQueryObject;
import cn.wolfcode.crm.service.ICheckingManageService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("check")
public class CheckController {

    @Autowired
    private ICheckingManageService checkingManageService;

    @RequestMapping("view")
    public String view(){
        return "check/check";
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(CheckingManageQueryObject qo) {
        PageResult pageResult = checkingManageService.query(qo);
        return pageResult;
    }

    @ResponseBody
    @RequestMapping("check")
    @RequiresPermissions(value = {"check:check","签到"},logical = Logical.OR)
    public JSONResult check(CheckingManage checkingManage){
        try {
            checkingManageService.checkin(checkingManage);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("不需要再签到了");
        }
    }

    @ResponseBody
    @RequestMapping("checkout")
    @RequiresPermissions(value = {"check:checkout","签退"},logical = Logical.OR)
    public JSONResult checkout(CheckingManage checkingManage){
        try {
            checkingManageService.checkout(checkingManage);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("已签退不需要再签退");
        }
    }

    @ResponseBody
    @RequestMapping("supplement")
    @RequiresPermissions(value = {"check:supplement","补签"},logical = Logical.OR)
    public JSONResult suppleMent(CheckingManage checkingManage, HttpServletRequest request){
        try {
            String sup = request.getParameter("sup");
            checkingManageService.supplement(checkingManage,sup);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("已签到,无需补签");
        }
    }



}
